package com.pug.web.interceptor.context;

/**
 * @author RenGaoshuai
 * @date 2023/7/6 14:50
 * @description 自定义上下文信息
 */
public class HeaderContext {

    private static final ThreadLocal<HeaderContext> CONTEXT = new ThreadLocal<>();

    private String clientId;

    public String getClientId() {
        return clientId;
    }

    public HeaderContext(HeaderContextBuild build) {
        this.clientId = build.clientId;
        saveContext(this);
    }

    /**
     * 保存上下文
     */
    public void saveContext(HeaderContext context) {
        CONTEXT.set(context);
    }

    /**
     * 获取上下文
     */
    public static HeaderContext getInstance() {
        return CONTEXT.get();
    }

    /**
     * 清除上下文
     */
    public static void cleanContext() {
        CONTEXT.remove();
    }

    public static class HeaderContextBuild {

        private String clientId;

        public HeaderContextBuild clientId(String clientId) {
            this.clientId = clientId;
            return this;
        }

        public void build() {
            new HeaderContext(this);
        }

    }


}
